<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/7
 * Time: 19:12
 */

namespace App\Game\Traits;


trait PtsTrait
{
    public function initHeader()
    {
        $token = $this->getToken();
        $this->header = [
            "Accept" => "application/json",
            "Content-Type" => "application/json",
            "X-ACCESS-TOKEN" => $token
        ];
    }

    public function getToken()
    {
        $this->header = [
            "Accept" => "application/json",
            "Content-Type" => "application/json",
        ];
        $params = [
            'secretKey' => $this->secretKey,
            'username' => $this->username,
            'password' => $this->password
        ];
        $json = [
            'json' => $params,
        ];
        $this->header = array_merge($this->header, ['Content-Length' => strlen(json_encode($params))]);
        $url = $this->gameUrl . self::LOGIN;
        $output = $this->request('POST', $url, [], $this->header, $json);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['accessToken'])) {
            return $outputarr['accessToken'];
        } else {
            return false;
        }
    }
}